package com.triones.api.service.interlace.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.triones.api.entity.interlace.dto.InterlaceCardBillStatisticsDO;
import com.triones.api.entity.interlace.dto.InterlaceInfinityCardDO;
import com.triones.api.mapper.interlace.InterlaceCardBillStatisticsMapper;
import com.triones.api.mapper.interlace.InterlaceInfinityCardMapper;
import com.triones.api.service.interlace.InterlaceCardBillStatisticsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * interlace 账单统计
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class InterlaceCardBillStatisticsServiceImpl implements InterlaceCardBillStatisticsService {

    private final InterlaceCardBillStatisticsMapper interlaceCardBillStatisticsMapper;

    private final InterlaceInfinityCardMapper interlaceInfinityCardMapper;

    @Override
    public void increaseAmount(String cardId, BigDecimal amount) {
        InterlaceCardBillStatisticsDO billStatisticsDO = interlaceCardBillStatisticsMapper.selectOne(new LambdaQueryWrapper<InterlaceCardBillStatisticsDO>()
            .eq(InterlaceCardBillStatisticsDO::getCardId, cardId)
            .eq(InterlaceCardBillStatisticsDO::getBillYear, DateUtils.getNowYearStr())
            .eq(InterlaceCardBillStatisticsDO::getBillMonth, DateUtils.getNowMonthWithZero())
            .last("limit 1 FOR UPDATE"));
        if (billStatisticsDO == null) {
            billStatisticsDO = new InterlaceCardBillStatisticsDO();
            billStatisticsDO.setCardId(cardId);
            billStatisticsDO.setBillYear(DateUtils.getNowYearStr());
            billStatisticsDO.setBillMonth(DateUtils.getNowMonthWithZero());
            billStatisticsDO.setStartDay("01");
            billStatisticsDO.setEndDay(DateUtils.getMaxDayOfMonthStr());
            billStatisticsDO.setAmount(BigDecimal.ZERO);
            billStatisticsDO.setLastDate(System.currentTimeMillis());
            interlaceCardBillStatisticsMapper.insert(billStatisticsDO);
            interlaceCardBillStatisticsMapper.increaseAmount(billStatisticsDO.getId(), amount, System.currentTimeMillis());
        } else {
            interlaceCardBillStatisticsMapper.increaseAmount(billStatisticsDO.getId(), amount, System.currentTimeMillis());
        }
    }

    @Override
    public void decreaseAmount(String cardId, BigDecimal amount) {
        InterlaceCardBillStatisticsDO billStatisticsDO = interlaceCardBillStatisticsMapper.selectOne(new LambdaQueryWrapper<InterlaceCardBillStatisticsDO>()
            .eq(InterlaceCardBillStatisticsDO::getCardId, cardId)
            .eq(InterlaceCardBillStatisticsDO::getBillYear, DateUtils.getNowYearStr())
            .eq(InterlaceCardBillStatisticsDO::getBillMonth, DateUtils.getNowMonthWithZero())
            .last("limit 1 FOR UPDATE"));
        if (billStatisticsDO == null) {
            billStatisticsDO = new InterlaceCardBillStatisticsDO();
            billStatisticsDO.setCardId(cardId);
            billStatisticsDO.setBillYear(DateUtils.getNowYearStr());
            billStatisticsDO.setBillMonth(DateUtils.getNowMonthWithZero());
            billStatisticsDO.setStartDay("01");
            billStatisticsDO.setEndDay(DateUtils.getMaxDayOfMonthStr());
            billStatisticsDO.setAmount(BigDecimal.ZERO);
            billStatisticsDO.setLastDate(System.currentTimeMillis());
            interlaceCardBillStatisticsMapper.insert(billStatisticsDO);
            interlaceCardBillStatisticsMapper.decreaseAmount(billStatisticsDO.getId(), amount, System.currentTimeMillis());
        } else {
            interlaceCardBillStatisticsMapper.decreaseAmount(billStatisticsDO.getId(), amount, System.currentTimeMillis());
        }
    }
}
